package com.fancy.common.utils;

import java.util.Locale;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fancy.common.LoginContext;
import com.fancy.common.constant.i18n.BaseI18n;

/**
 * Spring容器工具类
 * 
 * @author xinfeng.hu 2013-3-13下午12:15:34
 * @version 1.0.0
 * @category 杭州广桥网络技术有限公司(商业先知)
 */
public final class SpringUtil implements ApplicationContextAware {

	private static ApplicationContext appContext = ContextLoader.getCurrentWebApplicationContext();

	/**
	 * 根据message code 获取对应国家的语言信息
	 * 
	 * @param messageCode
	 * @param args
	 * @return
	 */
	public static String getMessage(final BaseI18n messageCode, final Object... args) {
		return getMessage(messageCode, LoginContext.getLocale(), args);
	}

	/**
	 * 根据message code 获取对应国家的语言信息
	 * 
	 * @param messageCode
	 * @param locale
	 * @param args
	 * @return
	 */
	public static String getMessage(final BaseI18n messageCode, final Locale locale, final Object... args) {
		ApplicationContext appContext = SpringUtil.getAppContext();
		final String message = appContext.getMessage(messageCode.getMessageCode(), args, locale);
		if (StringUtils.isBlank(message)) {
			throw new RuntimeException(String.format("messageCode=%s not in i18n file", messageCode));
		}
		return message;
	}

	public static Object getBean(String beanName) {
		return appContext.getBean(beanName);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName, Class<T> clazz) {
		return (T) appContext.getBean(beanName);
	}

	/**
	 * 往spring容器中注册bean
	 * 
	 * @param id
	 *            bean id
	 * @param beanClass
	 *            注册的bean 类
	 */
	public static void registerBean(String id, Class<?> beanClass) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ((ConfigurableApplicationContext) appContext).getBeanFactory();
		registry.registerBeanDefinition(id, new AnnotatedGenericBeanDefinition(beanClass));
	}

	/**
	 * 判断容器中是否包含某个id类的实例
	 * 
	 * @param id
	 * @return
	 */
	public static boolean containsBean(String id) {
		return appContext.containsBean(id);
	}

	public static ApplicationContext getAppContext() {
		return appContext;
	}

	public static ApplicationContext getAppContext(final ServletContext servletContext) {
		return WebApplicationContextUtils.getWebApplicationContext(servletContext);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringUtil.appContext = applicationContext;
	}
}
